package com.example.guestbook.service;

import static junit.framework.Assert.assertEquals;

import java.util.List;

import org.junit.Test;

import com.example.guestbook.po.Greeting;
import com.gaeig.test.base.BaseDatastoreTest;

public class GuestBookServiceTest extends BaseDatastoreTest {
  private GuestBookService service = null;

  @Test
  public void testSaveGreetings() {
    service = getBean(GuestBookServiceImpl.class);
    service.saveGreeting(getGreeting());
  }

  @Test
  public void testGetAllGreetings() {
    service = getBean(GuestBookServiceImpl.class);
    List<Greeting> greetings = service.getAllGreetings();
    System.out.println(greetings.size());
  }

  @Test
  public void testTransaction() {
    service = getBean(GuestBookServiceImpl.class);
    int beforeSize = service.getAllGreetings().size();
    try {
      service.testTransaction(getGreeting());
    } catch (Exception e) {
      e.printStackTrace();
    }
    int afterSize = service.getAllGreetings().size();
    assertEquals(beforeSize, afterSize);
  }

  private Greeting getGreeting() {
    Greeting greeting = new Greeting();
    greeting.setContent("Hello World");
    greeting.updateChangeLog();
    return greeting;
  }
}
